package gptma.bo.state;

import gptma.bo.model.request.ClientRequest;
import gptma.bo.util.ModelConstants;

import java.util.HashMap;
import java.util.Map;

public class ClientRequestStateEngineImpl implements ClientRequestStateEngine {

	private static final Map<ClientRequestStateEngineKey, String> RFC_STATE_MAPPING;
	private static final Map<ClientRequestStateEngineKey, String> AR_STATE_MAPPING;

	static {
		RFC_STATE_MAPPING = new HashMap<ClientRequestStateEngineKey, String>();

		ClientRequestStateEngineKey key1 = new ClientRequestStateEngineKey(
				ModelConstants.RFC_EN_ATTENTE, ModelConstants.RFC_A_EVALUER);
		RFC_STATE_MAPPING
				.put(key1, ModelConstants.RFC_A_AFFECTER_EN_EVALUATION);

		ClientRequestStateEngineKey key2 = new ClientRequestStateEngineKey(
				ModelConstants.RFC_EN_ATTENTE_D_ACCORD,
				ModelConstants.RFC_A_REALISER);
		RFC_STATE_MAPPING.put(key2, ModelConstants.RFC_A_REALISER);

		ClientRequestStateEngineKey key3 = new ClientRequestStateEngineKey(
				ModelConstants.RFC_EN_ATTENTE, ModelConstants.RFC_ACCEPTEE);
		RFC_STATE_MAPPING
				.put(key3, ModelConstants.RFC_A_AFFECTER_EN_EVALUATION);

		AR_STATE_MAPPING = new HashMap<ClientRequestStateEngineKey, String>();

		ClientRequestStateEngineKey key4 = new ClientRequestStateEngineKey(
				ModelConstants.AR_A_CLORE, ModelConstants.AR_CLOTURE_REFUSEE);
		AR_STATE_MAPPING.put(key4, ModelConstants.AR_A_REALISER);

		ClientRequestStateEngineKey key5 = new ClientRequestStateEngineKey(
				ModelConstants.AR_EN_ATTENTE, ModelConstants.AR_ACCEPTE);
		AR_STATE_MAPPING.put(key5, ModelConstants.AR_A_REALISER);
	}

	@Override
	public String getNextAction(ClientRequest clientRequest, String lastState) {
		String ret = null;

		ClientRequestStateEngineKey key = new ClientRequestStateEngineKey(
				lastState, clientRequest.getClientStatus());

		switch (clientRequest.getRequestType()) {
		case DE:
			ret = RFC_STATE_MAPPING.get(key);
			break;
		case RA:
			ret = AR_STATE_MAPPING.get(key);
			break;

		default:
			break;
		}

		if (ret == null) {
			ret = ModelConstants.ACTION_NO_ACTION;
		}

		return ret;
	}

}
